package moduleFrame
{
	import flash.events.Event;
	import flash.utils.getQualifiedClassName;
	
	import moduleFrame.interfaces.IMapComposite;
	import moduleFrame.interfaces.IMaper;
	import moduleFrame.utils.IRichDispatcher;
	import moduleFrame.utils.RichEventDispatcher;
	
	/**
	 * 用于被映射的对象
	 * @author Pelephone
	 */
	internal class Maper implements IMaper
	{
		protected var _eventDispatcher:IRichDispatcher;
		
		protected var _parent:IMapComposite;
		protected var _root:IMapComposite;
		
		/**
		 * 唯一id,绑定在module上面的名字
		 */
		private var _name:String;
		
		public function Maper(aliasName:String=null)
		{
			if(aliasName)
				_name = aliasName;
			else
				_name = getQualifiedClassName(this);
			
			_eventDispatcher = new RichEventDispatcher(this);
		}
		
		
		//////////////////////////
		// public方法
		//////////////////////////
		
		
		public function setName(value:String):void
		{
			_name = value;
		}
		
		public function getName():String
		{
			return _name;
		}
		
		public function setMapParent(mapParent:IMapComposite):void
		{
			_parent = mapParent;
			_root = mapParent.getMapRoot();
		}
		
/*		public function safeRemove():void
		{
			_root = null;
			if(!_parent) return;
			_parent.removeSingletonOf(this);
			_parent = null;
		}*/
		
		/**
		 * 安全移除
		 * @param c
		 
		public function safeRemove(c:IMaper):void
		{
			if (c.getComposite()) {
				c.remove(c);
			} else {
				c.removeParentRef();
			}
		}*/
		
		/**
		 * 注册时触发,将对象映射在mapParent上面
		 */ 
		public function onRegister():void
		{
			// 覆盖此方法
		}
		
		/**
		 * 移出绑定时触发
		 */ 
		public function onRemove( ):void
		{
			
		}
		
		public function mapTarget():IMaper
		{
			return this;
		}
		
		
		public function dispose():void
		{
			eventDispatcher.removeAllListeners();
			_eventDispatcher = null;
			_parent = null;
			_root = null;
		}
		
		/**
		 * 映射组合的父类
		 */
		public function getMapParent():IMapComposite
		{
			return _parent;
		}
		
		/**
		 * 映射组合的根类
		 */
		public function getMapRoot():IMapComposite
		{
			if(!_root) return Facade.getInstance();
			return _root;
		}
		
		////////////////////////////////
		// 事件相关
		////////////////////////////////
		
		
		/**
		 * @inheritDoc
		 */
		public function get eventDispatcher():IRichDispatcher
		{
			return _eventDispatcher;
		}
		
		/**
		 * @private
		 */
		public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, 
										 priority:int = 0, useWeakReference:Boolean = false):void
		{
			eventDispatcher.addEventListener(type, listener, useCapture, priority);
		}
		
		/**
		 * @private
		 */
		public function dispatchEvent(event:Event):Boolean
		{
//			return eventDispatcher.dispatchEvent(event);
			if(eventDispatcher.hasEventListener(event.type))
				return eventDispatcher.dispatchEvent(event);
			return false;
		}
		
		/**
		 * @private
		 */
		public function hasEventListener(type:String):Boolean
		{
			return eventDispatcher.hasEventListener(type);
		}
		
		/**
		 * @private
		 */
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
		{
			eventDispatcher.removeEventListener(type, listener, useCapture);
		}
		
		/**
		 * @private
		 */
		public function willTrigger(type:String):Boolean
		{
			return eventDispatcher.willTrigger(type);
		}
	}
}